package com.lmscn.lms.api.controller;

import com.jfinal.core.Controller;
import com.lmscn.lms.api.model.*;
import com.lmscn.lms.api.util.JsonKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Czl on 2015/12/24.
 */
public class TestPaperController extends Controller {

    public void testPaper() {
        Long testPaperId = getParaToLong("testpaperId");

        Map<String, Object> finalMap = new HashMap<String, Object>();//最终容器
        Map<String, Object> dataMapList = new HashMap<String, Object>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        TestpaperResult testpaperResult = TestpaperResult.dao.findBytestPaperId(testPaperId);
        //获取试卷都有哪些题目
        List questionList = TestpaperItem.dao.getQuestionList(testPaperId);
        //获取学员已经答了哪些题。
        List studentQuestionList = TestpaperItemResult.dao.getQuestionList(testPaperId, testpaperResult.getLong("student_id"));
        //把选项赋给题目
        List questions = new ArrayList();
        for (int i = 0; i < questionList.size(); i++) {
            Question question = (Question) questionList.get(i);
            List questionMetasList = QuestionMetas.dao.getQuestionMetas(Long.valueOf(question.getBigInteger("id").toString()));
            for (int j = 0; j < questionMetasList.size(); j++) {
                QuestionMetas questionMetas = (QuestionMetas) questionMetasList.get(j);
                question.set("item" + (j + 1) + "_id", questionMetas.get("options"));
            }
            questions.add(question);
        }
        Map<String, Object> questionsMap = new HashMap<String, Object>();
        questionsMap.put("question", questions);
        questionsMap.put("answered", studentQuestionList);
        dataMapList.put("questions", questionsMap);
        dataMap.put("answeredNum", studentQuestionList.size());
        dataMap.put("limited_time", testpaperResult.getInt("limited_time"));
        dataMap.put("used_time", testpaperResult.getInt("used_time"));
        dataMap.put("questionAbout", dataMapList);
        finalMap.put("testPaper", dataMap);
        renderJson(JsonKit.mapToJson(finalMap, 39));

    }
}
